package com.pactera.madp.cp.controller.oms.channel;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pactera.madp.common.core.exception.CheckedException;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.common.log.annotation.SysLog;
import com.pactera.madp.cp.api.dto.channel.*;
import com.pactera.madp.cp.api.vo.channel.ChannelCodeDetailsVO;
import com.pactera.madp.cp.api.vo.channel.ChannelCodeEmployeeListVO;
import com.pactera.madp.cp.api.vo.channel.ChannelCodeListVO;
import com.pactera.madp.cp.api.vo.channel.ChannelCodePageVO;
import com.pactera.madp.cp.handler.CorpContextHolder;
import com.pactera.madp.cp.service.ChannelCodeContactStatisticsService;
import com.pactera.madp.cp.service.IChannelCodeService;
import com.pactera.madp.cp.service.IWorkContactEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.List;

/**
 * @author: yangpengwei
 * @time: 2021/2/22 2:47 下午
 * @description 渠道活码
 */
@RestController
@AllArgsConstructor
@RequestMapping("/channelcode")
@Api(value = "channelcode", tags = "渠道活码")
public class ChannelCodeController {

    private IChannelCodeService channelCodeService;
    private ChannelCodeContactStatisticsService channelCodeContactStatisticsService;

    /**
     * @author: yangpengwei
     * @time: 2021/2/22 3:53 下午
     * @description 新建渠道码
     */
    @SysLog("新建渠道码")
    @PostMapping
    @ApiOperation(value = "新建渠道码")
    public R<Boolean> storeCode(@RequestBody ChannelCodeAddDTO channelCodeAddDTO) {
        if (channelCodeService.createChannelCode(channelCodeAddDTO)) {
            channelCodeContactStatisticsService.saveContact(CorpContextHolder.getCorpId());
            return R.ok(Boolean.TRUE, "新增成功");
        } else {
            return R.failed(Boolean.FALSE, "新增失败") ;
        }
    }

  /**
     * @author: yangpengwei
     * @time: 2021/2/22 3:53 下午
     * @description 编辑渠道码
     */
    @SysLog("编辑渠道码")
    @PutMapping
    @ApiOperation(value = "编辑渠道码")
    public R<Boolean> updateCode(@RequestBody ChannelCodeUpdateDTO channelCodeUpdateDTO) {
        return channelCodeService.updateChannelCode(channelCodeUpdateDTO) ? R.ok(Boolean.TRUE, "修改成功") : R.failed(Boolean.FALSE, "修改失败");
    }

    /**
     * @author: yangpengwei
     * @time: 2021/2/22 3:53 下午
     * @description 展示渠道码
     */
    @GetMapping("/{channelCodeId}")
    @ApiOperation(value = "展示渠道码")
    @ApiImplicitParams({
            @ApiImplicitParam(name="channelCodeId", value="渠道活码id", dataType = "Integer")
    })
    public R<ChannelCodeDetailsVO> showCode(@PathVariable Integer channelCodeId) {
        if(channelCodeId == null) {
            throw new CheckedException("渠道码 id 不能为空");
        }
        return R.ok(channelCodeService.channelCodeDetailsById(channelCodeId));
    }

    /**
     * @author: yangpengwei
     * @time: 2021/2/22 3:53 下午
     * @description 分页查询渠道码
     */
    @GetMapping("/page")
    @ApiOperation(value = "渠道码列表")
    public R<IPage<ChannelCodePageVO>> codeList(ChannelCodePageDTO dto, Page page) {
        return R.ok(channelCodeService.pageList(dto, page));
    }

    @SysLog("删除渠道活码")
    @DeleteMapping
    @ApiOperation(value = "删除渠道活码")
    public R<Boolean> deleteChannelCode(Integer id) {
        return channelCodeService.deleteChannelCode(id) ? R.ok(Boolean.TRUE, "删除成功") : R.failed(Boolean.FALSE, "删除失败");
    }

    @SysLog("批量导出渠道活码")
    @PostMapping("/exit-zip")
    @ApiOperation(value = "批量导出渠道活码")
    public void exitImagesByZip(@RequestBody ChannelCodeExitImageDTO dto, HttpServletResponse response, OutputStream out) {
        channelCodeService.exitImagesByZip(dto, response, out);
    }

    @GetMapping("/list")
    @ApiOperation(value = "渠道活码与渠道活码分组列表")
    @ApiImplicitParam(name="keyWords", value="模糊搜索名", dataType = "String")
    public R<List<ChannelCodeListVO>> getList(String keyWords) {
        return R.ok(channelCodeService.getList(keyWords));
    }

    @GetMapping("/list-employee")
    @ApiOperation(value = "获取当前渠道活码及渠道活码分组所有成员列表")
    public R<List<ChannelCodeEmployeeListVO>> getChannelCodeEmployeeList(ChannelCodeEmployeeListDTO dto) {
        return R.ok(channelCodeService.getChannelCodeEmployeeList(dto));
    }

}
